<template>
	<view class="content">
		<view v-if="isLogin">
			<image class="headicon" @click="changeHeadIcon" :src="'http://localhost:8090/foodapp/'+userInfo.icon">
			</image>
			<view class="text-area" v-if="isLogin">
				<text class="title">{{userInfo.nickname}}</text>
			</view>
		</view>

		<view v-if="!isLogin">
			<image class="headicon" :src="userInfo.icon"></image>
			<view class="text-area">
				<button type="primary" size="mini" @click="toLogin">点击登录</button>
			</view>
		</view>

		<view style="margin: 20rpx; width: 100%;">
			<view class="entry">
				个人信息
				<!-- <image src="../../static/more.png"></image> -->
			</view>
			<view class="entry">
				订单列表
				<!-- <image src="../../static/more.png"></image> -->
			</view>
			<view class="entry">
				修改密码
				<!-- <image src="../../static/more.png"></image> -->
			</view>
			<view class="entry" @click="toPage">
				意见反馈
				<!-- <image src="../../static/more.png"></image> -->
			</view>
		</view>
		<button type="primary" size="default" style="width: 100%;" :disabled="!isLogin" @click="logout">退出</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				userInfo: {
					userid: null,
					icon: '',
					nickname: '',
					username: null

				},
				isLogin: false
			}
		},
		onLoad() {

		},
		onShow() {
			this.getUserInfo();
		},
		methods: {
			changeHeadIcon() {
				
				uni.chooseImage({

					success: (res) => {
						if(res&&res.tempFilePaths&&res.tempFilePaths.length>0){
						let filePath = res.tempFilePaths[0]
						console.log(res);
						uni.uploadFile({
							url: 'http://localhost:8090/foodapp/user/upload_headpic',
							filePath: filePath,
							name: 'file',
							formData: {
								'id':this.userInfo.id
							},
							success: (uploadRes) => {
								console.log(uploadRes);
								let resData = JSON.parse(uploadRes.data)

								this.userInfo.icon=resData.data.icon
								
							},
							fail: (err) => {
								console.log(err);
							}
							})
						}
					}
					
				})
				},
				logout() {
					uni.setStorageSync('userInfo', null)
					this.getUserInfo()
				
				},
				toLogin() {
					uni.navigateTo({
						url: './login'
					})
				},
				getUserInfo() {
					let userInfo = uni.getStorageSync('userInfo')
					if (userInfo) {
						this.userInfo = userInfo
						this.isLogin = true
						console.log('用户已登录，读取成功');
					} else {
						this.userInfo.username = null
						this.userInfo.nickname = null
						this.userInfo.icon = '../../static/cat-8.png'
						this.isLogin = false
					}
				},
				toPage() {
					uni.navigateTo({
						url: '../comment/addcomment'
					})
				},
			},
			
			
		}

	
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		padding: 20rpx;
	}

	.headicon {
		height: 200rpx;
		width: 200rpx;
		margin-top: 80rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
		border-radius: 50%;
	}

	.text-area {
		display: flex;
		justify-content: center;
	}

	.title {
		font-size: 36rpx;
		color: #8f8f94;
	}

	.entry {
		width: 100%;
		text-align: left;
		height: 3em;
		line-height: 3em;
		text-indent: 1em;
		border-top: #CCC solid 1rpx;
	}

	.entry:last-of-type {
		border-bottom: #CCC solid 1rpx;
	}

	.entry::after {
		content: '';
		background-image: url('../../static/more.png');
		position: absolute;
		width: 50rpx;
		height: 50rpx;
		margin-top: 0.5em;
		background-size: contain;
		right: 50rpx;
	}
</style>